1.使用注解实现 aop
- 配置 xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <aop:aspectj-autoproxy/> <bean id="user" class="aop1.User"/> <bean id="improve" class="aop1.Improve"/>
</beans> ``` * 在增强类中注解 ```java package aop1;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint; import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Aspect public class Improve { @Before(value="execution(* aop1.User.say(..))") public void before_say(){ System.out.println("before"); }
public void after_say(){ System.out.println("after"); }
public void around_say(MethodInvocationProceedingJoinPoint joinPoint) throws Throwable { System.out.println("before"); joinPoint.proceed(); System.out.println("after"); } }
|
2.JdbcTemplate
1.增
1 2 3 4 5 6 7 8 9 10 11
| DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///JAVA"); dataSource.setUsername("root"); dataSource.setPassword("12345678"); JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource); String sql="insert into user values(?,?)"; jdbcTemplate.update(sql,1,"lwen");
|
2.删
还是 update 操作只是 sql 不一样
3.改
同增加
4.查
类似于 QueryRunner 也是使用接口的实例,不过这里没有准备好的实现类,实现类需要我们自己书写。
1.查询单值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| @Test void fun2(){ DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///JAVA"); dataSource.setUsername("root"); dataSource.setPassword("12345678"); JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource); String sql="select count(*) from user"; Object object=jdbcTemplate.queryForObject(sql,Integer.class); System.out.println(object); }
class MyRowMapper implements RowMapper{
public Object mapRow(ResultSet resultSet, int i) throws SQLException { User user=new User(); user.setId(resultSet.getInt("id")); user.setName(resultSet.getString("name")); return user; } }
@Test void fun3(){ DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///JAVA"); dataSource.setUsername("root"); dataSource.setPassword("12345678"); JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource); String sql="select * from user where name=?"; Object object=jdbcTemplate.queryForObject(sql, new MyRowMapper(),"lwen"); System.out.println(object); }
|
2.多值查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class MyRowMapper1 implements RowMapper{
public Object mapRow(ResultSet resultSet, int i) throws SQLException { User user=new User(); user.setId(resultSet.getInt("id")); user.setName(resultSet.getString("name")); return user; } } @Test void fun4(){ DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///JAVA"); dataSource.setUsername("root"); dataSource.setPassword("12345678"); JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource); String sql="select * from user"; List<User> users=jdbcTemplate.query(sql,new MyRowMapper1()); System.out.println(users); }
|
2.事务管理
1.xml方式配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="add"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="point1" expression="execution(* com.lwen.Service.add(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="point1"/> </aop:config> </beans>
|
2.注解方式
- 开启注解
1 2
| <tx:annotation-driven transaction-manager="transactionManager"/>
|
- 在事务类上面写注解
@Transactional